﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;

namespace cn.Util
{
    /// <summary>
    /// http请求
    /// </summary>
    public class HttpPost
    {
        /// <summary>
        /// 发起HTTP请求
        /// </summary>
        /// <param name="web"></param>
        /// <param name="postdata"></param>
        /// <returns></returns>
        public bool Post(string web, out string responseText, Dictionary<string,string> dic)
        {
            string postdata = "";
            foreach (var key in dic.Keys)
            {
              postdata+=(key + "=" + dic[key]+"&");
            }
     

            return PostX(web, "UTF-8", out responseText, postdata);
        }

        /// <summary>
        ///  发起HTTP请求
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="strPostdata"></param>
        /// <param name="strEncoding"></param>
        /// <returns></returns>
        public bool PostX(string URL, string strEncoding, out string responseText, string strPostdata)
        {
            bool isOk = false;
            responseText = "";
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "Post";
            request.Accept = "application/json";

            request.ContentType = "application/x-www-form-urlencoded";

            byte[] buffer = encoding.GetBytes(strPostdata);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                isOk = true;
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }

            using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
            {
                responseText = reader.ReadToEnd();
            }


            return isOk;
        }


        /// <summary>
        /// 发起HTTP请求
        /// </summary>
        /// <param name="web"></param>
        /// <returns></returns>
        public  string Get(string web) 
        {
           return  Get(web, System.Text.Encoding.GetEncoding("UTF-8"));
        }

        /// <summary>
        /// 发起HTTP请求
        /// </summary>
        /// <param name="web"></param>
        /// <param name="encodings"></param>
        /// <returns></returns>
        public string Get(string web, Encoding encodings)
        {
            string outs = "";
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(web);

            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                Stream newStream = wr.GetResponseStream();
                HttpWebResponse webResponse = (HttpWebResponse)wr;
                bool bRetVal = true;
                if (webResponse.StatusCode != HttpStatusCode.OK)
                    bRetVal = false;

                if (bRetVal)
                {
                    //创建接收回馈的字节流类
                    Stream receiveStream = webResponse.GetResponseStream();//得到回写的字节流
                    StreamReader readStream = new StreamReader(receiveStream, encodings);
                    outs = readStream.ReadToEnd();
                    readStream.Close();
                }
                if (webResponse != null)
                    webResponse.Close();
                //在这里对接收到的页面内容进行处理
                return outs;
            }

        }
    }
}
